package com.k.generator;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.*;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

/**
 * mybatis反向工具，可以使用此生成所需代码。
 * 必须有 c:/test/gen/java c:/test/gen/resources 文件夹
 *
 * @author dongjian
 * */
public class MyBatisGeneratorTest {

    private String confFilePath = "generatorConfigb.xml";

    public final void testGenerator() throws Exception {

        List<String> warnings = new ArrayList<>();

        confFilePath = this.getClass().getResource(confFilePath).getFile();
        System.out.println("configPath:"+confFilePath);

        File configFile = new File(confFilePath);

        Configuration config = new ConfigurationParser(warnings).parseConfiguration(configFile);

        DefaultShellCallback callback = new DefaultShellCallback(true);

        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);

        System.out.println("warn::"+warnings);

        myBatisGenerator.generate(null);
    }
    public final void gen2() throws XMLParserException, IOException, InvalidConfigurationException, SQLException, InterruptedException {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File( this.getClass().getResource("generatorConfigb.xml").getFile());
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);

        Context ctx = config.getContexts().get(0);
        // UserModel.java
        JavaModelGeneratorConfiguration modelJava = ctx.getJavaModelGeneratorConfiguration();
        System.out.println("modelJava:"+modelJava.getTargetPackage()+" :"+modelJava.getTargetProject());
        // UserMapper.java
        JavaClientGeneratorConfiguration mapperJava = ctx.getJavaClientGeneratorConfiguration();
        System.out.println("mapperJava:"+mapperJava.getTargetPackage()+" :"+mapperJava.getTargetProject());

        SqlMapGeneratorConfiguration mapperXml = ctx.getSqlMapGeneratorConfiguration();
        System.out.println("mapperXml:"+mapperXml.getTargetPackage()+" :"+mapperXml.getTargetProject()+" :");

        // origin table
        List<TableConfiguration> tableList = ctx.getTableConfigurations();

        // add ext table
        TableConfiguration myconfigTable = new TableConfiguration(ctx);
        myconfigTable.setTableName("duty_grade_info");
        myconfigTable.setDomainObjectName("DutyGradeInfo");

        myconfigTable.setCountByExampleStatementEnabled(false);
        myconfigTable.setDeleteByExampleStatementEnabled(false);
        myconfigTable.setUpdateByExampleStatementEnabled(false);
        myconfigTable.setSelectByExampleStatementEnabled(false);
        myconfigTable.getProperties().setProperty("useActualColumnNames","false");

        ctx.getTableConfigurations().add(myconfigTable);


        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    }
    public static void main(String[] args) {
        try {
            new MyBatisGeneratorTest().gen2();
            System.out.println("===完成");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}  